<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">

            <!-- 放置表单 -->
            <el-form ref="refuserInfo" :model="userInfo" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password">
                <el-input v-model="userInfo.password" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="savaUser">保存</el-button>
                <el-button>取消</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${userId}?type=personal`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <component :is="userComponent" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <el-row type="flex" justify="end">
              <el-tooltip content="打印岗位信息">
                <router-link :to="`/employees/print/${userId}?type=job`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <component :is="jobComponent" />
          </el-tab-pane></el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDateilById } from '@/api/user'
import { savaEmployee } from '@/api/employees'
import UserInfo from '../components/user-info'
import JobInfo from '../components/job-info'
export default {
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      userId: this.$route.params.id,
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { required: true, min: 1, max: 4, message: '请填入1-4位姓名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { required: true, min: 6, max: 16, message: '请填入6-16位密码', trigger: 'blur' }
        ]
      },
      userComponent: 'UserInfo',
      jobComponent: 'JobInfo'
    }
  },
  created() {
    this.loadgetUserDateilById()
  },
  methods: {
    // 根据员工id获取信息
    async loadgetUserDateilById() {
      this.userInfo = await getUserDateilById(this.userId)
      console.log(this.userInfo)
    },
    // 保存员工信息
    savaUser() {
      this.$refs.refuserInfo.validate().then(async() => {
        await savaEmployee(this.userInfo)
        this.$message.success('修改成功')
      })
    }

  }
}
</script>

<style>

</style>
